(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/Script/utils/AudioUtils.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, '03ccbHeGIRCqr5+rA7e2OBZ', 'AudioUtils', __filename);
// Script/utils/AudioUtils.js

"use strict";

/**
 * 音频工具类 
 * yanglip
 */
var self = void 0;
var au = cc.Class({
    init: function init() {
        self = this;
        self._playMusic = {}; // 缓存音乐，{name: ID}
        self._playEffect = {}; // 缓存音效，{name: ID}
        self._switchMusic = false; // 音乐开关
        self._musicVolume = 1; // 音乐音量
        self._switchEffect = false; // 音效开关
        self._effectVolume = 1; // 音效音量

        // 获取本地设置音量大小
        var audioSetting = JSON.parse(cc.sys.localStorage.getItem("audio"));
        if (audioSetting == undefined) {

            cc.sys.localStorage.setItem("audio", JSON.stringify({ 'effect': 1, 'music': 1 }));
            audioSetting = JSON.parse(cc.sys.localStorage.getItem("audio"));
        }
        self._effectVolume = audioSetting["effect"];
        self._musicVolume = audioSetting["music"];

        //获取本地开关设置
        var switchSetting = JSON.parse(cc.sys.localStorage.getItem("audioSwitch"));
        if (switchSetting == undefined) {
            cc.sys.localStorage.setItem("audioSwitch", JSON.stringify({ 'switchMusic': true, 'switchEffect': true }));
            switchSetting = JSON.parse(cc.sys.localStorage.getItem("audioSwitch"));
        }
        self.initSwitch(switchSetting["switchMusic"], switchSetting["switchEffect"]);
    },

    /**
     * 初始化音乐，音效开关
     */

    initSwitch: function initSwitch(switchMusic, switchEffect) {
        self._switchMusic = switchMusic;
        self._switchEffect = switchEffect;
    },

    /**
     * 加载文件夹下所有音频资源
     * url: 资源所在文件夹
     */
    reLoadRes: function reLoadRes(url) {
        cc.loader.loadResDir(url, cc.AudioClip, function (err, res) {
            //console.log(err);
            //console.log(res);
            if (err) {
                cc.error("【音频】资源加载错误");
                return;
            }
            for (var key in res) {
                self._playEffect[url] = res[key];
            }
        });
    },

    /**
     * 播放音效文件
     * url: 音效文件相对地址
     * loop: 是否循环播放
     */
    playEffect: function playEffect(url) {
        var loop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

        if (self._switchEffect) {
            cc.loader.loadRes(url, cc.AudioClip, function (err, audioClip) {
                if (err) {
                    cc.warn("【音频】音效" + url + "文件不存在");
                } else {
                    cc.audioEngine.setEffectsVolume(self._effectVolume);
                    var effectId = cc.audioEngine.playEffect(audioClip, loop);
                    //console.log("EffectId::::::::::::"+effectId);
                    self._playEffect[url] = effectId;
                }
            });
        }
    },

    /**
     * 转换音效开关
     */
    switchEffectFunc: function switchEffectFunc(flag) {
        self._switchEffect = flag;
        if (!self._switchEffect) {
            self.setStopAllEffect();
        }
        cc.sys.localStorage.setItem("audioSwitch", JSON.stringify({ 'switchEffect': self._switchEffect, 'switchMusic': self._switchMusic }));
    },

    /**
     * 获取音效开关状态
     */
    getSwitchEffect: function getSwitchEffect() {
        return self._switchEffect;
    },

    /**
     * 设置音效声音大小
     * value: 0.0 - 1.0
     */
    setEffectVolume: function setEffectVolume(value) {
        self._effectVolume = value;
        cc.audioEngine.setEffectsVolume(value);
        cc.sys.localStorage.setItem("audio", JSON.stringify({ 'effect': self._effectVolume, 'music': self._musicVolume }));
    },

    /**
     * 获取音效大小
     * @return 0.0 - 1.0
     */
    getEffectVolume: function getEffectVolume() {
        return cc.audioEngine.getEffectsVolume();
    },
    getEffectVolume1: function getEffectVolume1() {
        return self._effectVolume;
    },

    /**
     * 暂停指定音效
     * url： 资源路径
     */
    setPauseEffect: function setPauseEffect(url) {
        var audio = self._playEffect[url];
        if (audio) {
            cc.audioEngine.pauseEffect(audio);
        } else {
            cc.error("1【音频】音效文件" + url + "不存在");
        }
    },

    /**
     * 暂停正在播放的所有音效
     */
    setPauseAllEffect: function setPauseAllEffect() {
        cc.audioEngine.pauseAllEffects();
    },

    /**
     * 恢复指定音效
     * url:资源路径
     */
    setResumeEffect: function setResumeEffect(url) {
        var audio = self._playEffect[url];
        if (audio) {
            cc.audioEngine.resumeEffect(audio);
        } else {
            cc.error("2【音频】音效文件" + url + "不存在");
        }
    },

    /**
     * 恢复当前所暂停的所有音效
     */
    setResumeAllEffect: function setResumeAllEffect() {
        cc.audioEngine.resumeAllEffects();
    },

    /**
     * 停止播放指定音效
     * url: 资源路径
     */
    setStopEffect: function setStopEffect(url) {
        var audio = self._playEffect[url];
        if (self._switchEffect) {
            if (audio) {
                cc.audioEngine.stopEffect(audio);
            } else {
                cc.error("3【音频】音效文件" + url + "不存在");
            }
        }
    },

    /**
     * 停止播放所有正在播放的音效
     */
    setStopAllEffect: function setStopAllEffect() {
        cc.audioEngine.stopAllEffects();
    },

    /**
     * 背景音乐播放
     * url: 资源路径
     * loop: 是否循环
     */
    playMusic: function playMusic(url) {
        var loop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

        if (self._switchMusic) {
            cc.loader.loadRes(url, cc.AudioClip, function (err, audioClip) {
                if (err) {
                    cc.warn("【音频】音效" + url + "文件不存在");
                } else {
                    cc.audioEngine.setMusicVolume(self._musicVolume);
                    var musicId = cc.audioEngine.playMusic(audioClip, loop);
                    //console.log("musicId::::::::::::"+musicId);
                    self._playMusic[url] = musicId;
                }
            });
        }
    },

    /**
     * 转换音乐按钮开关
     */
    switchMusicFunc: function switchMusicFunc(flag) {
        //self._switchMusic = !self._switchMusic;
        self._switchMusic = flag;
        if (self._switchMusic) {
            self.setResumeMusic();
        } else {
            self.setPauseMusic();
        }
        cc.sys.localStorage.setItem("audioSwitch", JSON.stringify({ 'switchEffect': self._switchEffect, 'switchMusic': self._switchMusic }));
    },

    /**
     * 获取音乐开关状态
     */
    getSwitchMusic: function getSwitchMusic() {
        return self._switchMusic;
    },

    /**
     * 暂停当前播放音乐
     */
    setPauseMusic: function setPauseMusic() {
        cc.audioEngine.pauseMusic();
    },

    /**
     * 恢复当前被暂停音乐音乐
     */
    setResumeMusic: function setResumeMusic() {
        cc.audioEngine.resumeMusic();
    },

    /**
     * 重新播放该背景音乐
     */
    replayMusic: function replayMusic() {
        cc.audioEngine.rewindMusic();
    },

    /**
     * 暂停播放音乐
     * releaseData： 控制是否释放音乐资源 true释放资源 | false不释放资源
     */
    setStopMusic: function setStopMusic() {
        var releaseData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

        cc.audioEngine.stopMusic(releaseData);
    },

    setMusicVolume: function setMusicVolume(value) {
        self._musicVolume = value;
        cc.audioEngine.setMusicVolume(value);
        cc.sys.localStorage.setItem("audio", JSON.stringify({ 'effect': self._effectVolume, 'music': self._musicVolume }));
    },

    getMusicVolume: function getMusicVolume() {
        return cc.audioEngine.getMusicVolume();
    },

    getMusicVolume1: function getMusicVolume1() {
        return self._musicVolume;
    },

    /** 
     * 音乐是否正在播放（验证些方法来实现背景音乐是否播放完成）
     * return boolen
     */
    isMusicPlaying: function isMusicPlaying() {
        return cc.audioEngine.isMusicPlaying();
    },

    /**
     * 释放指定音效资源
     * url
     */
    releaseAudio: function releaseAudio(url) {
        var rawUrl = cc.url.raw(url);
        if (cc.loader.getRes(rawUrl)) {
            cc.audioEngine.unloadEffect(rawUrl);
        } else {
            cc.error("【音频】资源" + url + "不存在， 释放失败");
        }
    },

    releaseAllAudio: function releaseAllAudio() {
        cc.audioEngine.uncacheAll();
    }

});
au.inst = new au();

cc._RF.pop();
        }
        if (CC_EDITOR) {
            __define(__module.exports, __require, __module);
        }
        else {
            cc.registerModuleFunc(__filename, function () {
                __define(__module.exports, __require, __module);
            });
        }
        })();
        //# sourceMappingURL=AudioUtils.js.map
        